package com.his.system.manager.task;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5Util
 *
 * @Description
 * @Author HHJ
 * @Date 2020-07-31 15:03
 */
public class MD5Util {
    protected static char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    protected static MessageDigest messagedigest = null;

    public MD5Util() {
    }

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            System.err.println("MessageDigest初始化失败!");
        }
    }

    public static String getMD5(String string) {
        if (string == null) {
            return "";
        }
        return getMD5String(string.getBytes());
    }

    public static String getMD5String(byte[] bytes) {
        messagedigest.update(bytes);
        return bufferToHex(messagedigest.digest());
    }

    private static String bufferToHex(byte[] bytes) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte[] bytes, int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;

        for (int l = m; l < k; ++l) {
            char c0 = hexDigits[(bytes[l] & 240) >> 4];
            char c1 = hexDigits[bytes[l] & 15];
            stringbuffer.append(c0);
            stringbuffer.append(c1);
        }

        return stringbuffer.toString();
    }
}